package com.zghhost.codedemo.jvm;

import com.zghhost.codedemo.util.RuntimeUtil;
import com.zghhost.codedemo.util.StringUtil;

import java.util.LinkedList;
import java.util.List;

/**
 * @author guohua.zhang@zhaopin.com.cn
 * @Date 2019/1/16 11:06
 */
public class RuntimeApiTest {
    public static void main(String[] args){
        new RuntimeApiTest().test();
    }

    public void test(){
        System.out.println("开始运行...");
        printCurrentState();

        new Thread(){
            @Override
            public void run() {
                while (true){
                    try{sleep(1000);}catch (Exception e){}
                    printCurrentState();
                }
            }
        }.start();

        new Thread(){
            List<String> memcaster = new LinkedList<String>();
            @Override
            public void run() {
                while (true){
                    try{sleep(1);}catch (Exception e){}
                    memcaster.add(StringUtil.genFixedLengthString(64));
                }
            }
        }.start();
    }

    public static void printCurrentState(){
        System.out.printf(
                "%s %s %s \n"
                ,"当前系统总内存"+ RuntimeUtil.getTotalMemory()
                ,"当前系统最大内存"+ RuntimeUtil.getMaxMemory()
                ,"当前系统空闲内存"+ RuntimeUtil.getFreeMemory()
        );
    }
}
